package com.bbm.bbmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.entity.User;
import com.bbm.bbmanage.service.LoginService;
import com.bbm.bbmanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
        return loginService.login(user);
    }

    @GetMapping("/user/info")
    public ResponseResult getUserInfo(@RequestParam String username){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        User user = userService.getOne(queryWrapper.eq(User::getUsername, username));
        return ResponseResult.okResult(user);
    }

    @PostMapping("/user/register")
    public ResponseResult register(@RequestBody User user){
        return loginService.register(user);
    }

    @PostMapping("/user/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

    @PutMapping("/user/pwd")
    public ResponseResult updateUserPassword(@RequestBody User user) {
        return userService.updateUserPassword(user);
    }
}
